import subject from "@/config/mars3d";
import { ElMessage } from "element-plus";
import mars from "@/config/mars3d";
import storages from "../storage";
import { useStore } from "@/stores/index1";
const store = useStore();
import { useStoreData } from "@/stores/data";
const storeData = useStoreData();
/**
 * 标绘数据渲染器
 */
const renderer = (data: any) => {
    data.forEach((v: any) => {
        v.positions = JSON.parse(v.positions);
        v.style = JSON.parse(v.style);
        setGraphicToMap(v);
        // setTimeout(() => {
        //     if (store.tool.topBtn.coverage) {
        //         store.coverage = true;
        //         setTimeout(() => {
        //             setTreeData(v);
        //         }, 300);
        //     }
        // }, 1000);
    });
};
export default renderer;
/**
 * 添加树形菜单数据
 */
const setTreeData = (data: any) => {
    console.log(data);
    // 添加树菜单数据
    let treeData = {
        editor: true,
        id: data.dataId,
        isLocked: 0,
        name: data.name,
        pid: data.pid ? data.pid : data.folderId,
        show: data.isShow == 1 ? true : false,
        type: data.type,
        icon: data.icon,
    };
    (storeData.tree as any).append(treeData, treeData.pid);

    // 设为树菜单选中状态
    if (treeData.show) {
        setTimeout(() => {
            (storeData.tree as any).setChecked(treeData.id, true);
        }, 300);
    }
};
/**
 * 设置属性参数
 */
const setPropertyParameter = (data: any, graphic: any) => {
    graphic.setOptions({
        folderId: data.pid,
        icon: data.icon,
        plottingName: data.plottingName,
        plottingType: data.plottingType, // 标绘类型
        name: data.name,
        number: data.number,
        id: data.id,
    });
    graphic.show = data.isShow == "1" ? true : false;
    store.graphicLayer.addGraphic(graphic);
    data.dataId = graphic.id;
    // console.log(data, graphic);
    setTreeData(data);
    // 添加map数据
    // storages.set({
    //     coverageMap: {
    //         key: graphic.id,
    //         value: graphic,
    //     },
    // });
    // graphic.editing.restore() // 撤销编辑，可直接调用
};
const setGraphicToMap = (v: any) => {
    let graphic: any = undefined;
    // console.log(v);
    switch (v.type) {
        case "wall":
            /**
             * 立面文字-围栏
             */
            graphic = new subject.mars3d.graphic.WallEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "rectangle":
            /**
             * 贴地旋转文字 - "跑马文字" - 矩形
             */
            graphic = new subject.mars3d.graphic.RectangleEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            // 更新旋转文字属性
            if (v.plottingType == "rotateText") {
                let rotation = mars.Cesium.Math.toRadians(30);
                function getRotationValue() {
                    rotation += 0.005;
                    return rotation;
                }
                graphic.setStyle({
                    rotation: new mars.Cesium.CallbackProperty(getRotationValue, false),
                    stRotation: new mars.Cesium.CallbackProperty(getRotationValue, false),
                });
            }
            setPropertyParameter(v, graphic);
            break;

        case "label":
            /**
             * 普通文字
             */
            graphic = new subject.mars3d.graphic.LabelEntity({
                position: v.positions,
                style: v.style,
            });
            setPropertyParameter(v, graphic);
            break;

        case "billboard":
            /**
             * 图标点
             */
            graphic = new subject.mars3d.graphic.BillboardEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "polyline":
            /**
             * 线段
             */
            graphic = new subject.mars3d.graphic.PolylineEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "curve":
            /**
             * 曲线
             */
            graphic = new subject.mars3d.graphic.CurveEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "polylineVolume":
            /**
             * 管道
             */
            graphic = new subject.mars3d.graphic.PolylineVolumeEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "corridor":
            /**
             * 走廊
             */
            graphic = new subject.mars3d.graphic.CorridorEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "road":
            /**
             * 道路
             */
            graphic = new subject.mars3d.graphic.Road({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "plane":
            /**
             * 平面
             */
            graphic = new subject.mars3d.graphic.PlaneEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "circle":
            /**
             * 圆-圆柱
             */
            graphic = new subject.mars3d.graphic.CircleEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "polygon":
            /**
             * 多边形
             */
            graphic = new subject.mars3d.graphic.PolygonEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "regular":
            /**
             * 正多边形
             */
            graphic = new subject.mars3d.graphic.Regular({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "sector":
            /**
             * 扇形
             */
            graphic = new subject.mars3d.graphic.Sector({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "ellipse":
            /**
             * 椭圆-椭圆柱
             */
            graphic = new subject.mars3d.graphic.EllipseEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            console.log(graphic);
            setPropertyParameter(v, graphic);
            break;
        case "box":
            /**
             * 盒子
             */
            graphic = new subject.mars3d.graphic.BoxEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "cylinder":
            /**
             * 圆锥
             */
            graphic = new subject.mars3d.graphic.CylinderEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "ellipsoid":
            /**
             * 雷达及半球-球及椭球
             */
            graphic = new subject.mars3d.graphic.EllipsoidEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "xyz":
            /**
             * 金字塔瓦片
             */
            const tileLayer = new subject.mars3d.layer.XyzLayer({
                url: v.layerUrl,
                minimumLevel: 12,
                maximumLevel: 21,
                id: v.id,
                rectangle: { xmin: v.xmin, xmax: v.xmax, ymin: v.ymin, ymax: v.ymax },
            });
            tileLayer.show = v.isShow == "1" ? true : false;
            store.map.addLayer(tileLayer);
            // 添加map数据
            storages.set({
                coverageMap: {
                    key: tileLayer.id,
                    value: tileLayer,
                },
            });
            break;
        default:
            ElMessage.error(`${v.name}渲染错误`);
    }
};
